/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.amazon.s3;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jets3t.service.S3Service;
import org.jets3t.service.S3ServiceException;
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.model.S3Bucket;
import org.jets3t.service.model.S3Object;
import org.jets3t.service.security.AWSCredentials;

/**
 *
 * @author Ahmed Salem
 */
public class download_files {

    static S3Service s3Service = null;

    public static void connect() {
        String awsAccessKey = "AKIAJU7HCNXAM3LV7CZA";
        String awsSecretKey = "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";
        AWSCredentials awsCredentials =
                new AWSCredentials(awsAccessKey, awsSecretKey);

        try {
            s3Service = new RestS3Service(awsCredentials);
        } catch (S3ServiceException ex) {
            Logger.getLogger(download_files.class.getName()).log(Level.SEVERE, null, ex);
        }

        S3Bucket[] myBuckets = null;
        try {
            myBuckets = s3Service.listAllBuckets();
        } catch (S3ServiceException ex) {
            Logger.getLogger(download_files.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("How many buckets to I have in S3? " + myBuckets.length);

    }

    public static void downloadFile(String file_name,String Bucket_name) {
        {
            BufferedInputStream bis=new BufferedInputStream(null);
            BufferedOutputStream bos=new BufferedOutputStream(null);
            try {
                S3Bucket test_bucket = s3Service.getBucket(Bucket_name);
                S3Object objectComplete = s3Service.getObject(test_bucket, file_name);
                System.out.println("S3Object, complete: " + objectComplete);
                File ff = new File(objectComplete.getKey());
                
                 bis = new BufferedInputStream(objectComplete.getDataInputStream());
                 bos=new BufferedOutputStream(new FileOutputStream(ff.getName()));
                int i ;
                while( (i=bis.read())!= -1 ){
                    bos.write(i);
                }
                System.out.println("Greeting:");
            } catch (IOException ex) {
                Logger.getLogger(download_files.class.getName()).log(Level.SEVERE, null, ex);
            } catch (S3ServiceException ex) {
                Logger.getLogger(download_files.class.getName()).log(Level.SEVERE, null, ex);
            } finally
         {
            if (bis != null)
               try
               {
                  bis.close();
               }
               catch (IOException ioe)
               {
                  ioe.printStackTrace();
               }
            if (bos != null)
               try
               {
                  bos.close();
               }
               catch (IOException ioe)
               {
                  ioe.printStackTrace();
               }
         }


            
        }
    }


    public static void downloadFiles(String[]files,String Bucket_name) {
        for (int i = 0; i < files.length; i++) {
            downloadFile(files[i],Bucket_name);
        }
   }

     public static Long Size(String[]files) {
          Long size = null ;
        try {

            S3Bucket test_bucket = s3Service.getBucket("salem-backet");
            for (int i = 0; i < files.length; i++) {
                S3Object objectComplete = s3Service.getObject(test_bucket, files[i]);
                size=+objectComplete.getContentLength();
            }

        } catch (S3ServiceException ex) {
            Logger.getLogger(download_files.class.getName()).log(Level.SEVERE, null, ex);
        }
        return size;
     }
    public static void main(String[] args) {
        connect();
        System.out.println("------------- Download -----------------");

        System.out.println("size="+(Size(new String[]{"ahmed.tar.gz"}))+" byte");
    }
}
